﻿/*============================================
 * 类名 :MainChar
 * 描述 :记录并检验特殊字符的类
 *   
 * 创建时间: 2009-11-1 19:31:37
 *============================================*/
using System;
using System.Collections.Generic;

using System.Text;

namespace 命题证明
{
    /// <summary></summary>   
    static class MainChar
    {

        /// <summary>非 ┐</summary>
        public readonly static char Not = Convert.ToChar( 9488 ); //

        /// <summary>且，析取 ∧</summary>
        public readonly static char And = Convert.ToChar( 8743 ); //

        /// <summary>或，合取 ∨</summary>
        public readonly static char Or = Convert.ToChar( 8744 ); //

        /// <summary>正向推出  →</summary>
        public readonly static char Prove = Convert.ToChar( 8594 ); //

        /// <summary>双向推出  ↔</summary>
        public readonly static char ProveTwo = Convert.ToChar( 8596 ); //

        /// <summary>等值  ⇔</summary>
        public readonly static char Equal = Convert.ToChar( 8660 ); //⇔

        /// <summary>计算双目运算的值</summary>
        /// <param name="op">操作符</param>
        /// <param name="left">左值</param>
        /// <param name="right">右值</param>
        /// <returns>运算所得的值</returns>
        public static bool CalChar( Operator op, bool left, bool right )
        {
            if ( op == Operator.And )
            {
                return left && right;
            }
            else if ( op == Operator.Or )
            {
                return left || right;
            }
            else if ( op == Operator.Prove )
            {
                if ( left && !right )
                    return false;
                return true;
            }
            //这时应该忽略left值
            else if ( op == Operator.Not )
            {
                return right ^ true;
            } 
            else
            {
                return left ^ right;
            }
        }

        //是合法的字符
        public static bool IsValidateChar( char c )
        {
            if ( !IsWord( c ) && !IsOperator( c ) && !IsConst( c ) && !IsBracket( c ) )
                return false;
            return true;
        }

        /// <summary>是合法的操作符</summary> 
        public static bool IsOperator( char c )
        {
            if ( c == MainChar.And || c == MainChar.Equal || c == MainChar.Not || c == MainChar.Or || c == MainChar.Prove ||
                c == MainChar.ProveTwo )
                return true;
            return false;
        }
        /// <summary>是括号</summary> 
        public static bool IsBracket( char c )
        {
            if ( c == '(' || c == ')' )
            {
                return true;
            }
            return false;
        }
        /// <summary>是常数0，1</summary>
        public static bool IsConst( char c )
        {
            if ( c == '0' || c == '1' )
                return true;
            return false;
        }
        /// <summary>是合法的字母、操作数</summary>
        public static bool IsWord( char c )
        {
            c = char.ToLower( c );
            if ( 'a' <= c && c <= 'z' )
            {
                return true;
            }
            return false;
        }

    }

    /// <summary>
    /// 操作符
    /// </summary>
    enum Operator
    {
        /// <summary>非操作 ┐</summary>
        Not = 9488,

        /// <summary>且操作  ∧</summary>
        And = 8743,

        /// <summary>或操作  ∨</summary>
        Or = 8744,

        /// <summary>双推出操作  ↔</summary>
        ProveTwo = 8596,

        /// <summary>等值  ⇔</summary>
        Equal = 8660,

        /// <summary>正向推出 →</summary>
        Prove = 8594
    }

}
